import {
    reqRegister
} from "../../request/api"
//初始值
const initState = {
    user: {
        phone: "",
        nickname: "",
        password: "",
    }
}

//actionTypes
const types = {
    CHANGE_USER: "REGISTER_USER"
}

//reducer 修改数据
const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_USER:
            return {
                ...state,
                user: {
                    ...state.user,
                    [action.key]: action.value
                }
            }
            default:
                return state;
    }
}

//actionCreators组件派发的动作
export const actions = {
    changeUser: (key, value) => ({
        type: types.CHANGE_USER,
        key,
        value
    }),
    reqRegister: (history) => (dispatch, getState) => {
        let {user} = getState().register
        reqRegister(user).then(res => {
            if (res.data.code === 200) {
                history.push("/login")
            }
        })
    }
}

//selectors导出数据
export const getUser = (state) => state.register.user

export default reducer;